package com.company.emallbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.emallbackend.entity.Product;
import com.company.emallbackend.entity.Star;
import com.company.emallbackend.entity.User;
import com.company.emallbackend.exception.CommonException;
import com.company.emallbackend.mapper.StarMapper;
import com.company.emallbackend.service.IProductService;
import com.company.emallbackend.service.IStarService;
import com.company.emallbackend.utils.TokenUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hanlijian
 * @since 2023-01-12
 */
@Service
@Transactional
public class StarServiceImpl extends ServiceImpl<StarMapper, Star> implements IStarService {
    @Resource
    private IProductService productService;
    @Resource
    private StarMapper starMapper;
    
    @Override
    public void addProductStar(Integer productId) {
        if(Objects.isNull(productId)){
            throw new CommonException(201,"请检查您要点赞的商品");
        }
        User user = TokenUtils.getCurrentAdmin();
        if(Objects.isNull(user)){
            throw new CommonException(201,"请登录后点赞商品");
        }
        Product product = productService.getById(productId);
        if(Objects.isNull(product)){
            throw new CommonException(201,"系统繁忙,稍后再试");
        }
        //根据userId查询star表
        List<Star> starList = starMapper.selectList(new QueryWrapper<Star>().eq("user_id", user.getId()));
        Set<Integer> productSet = starList.stream().map(Star::getProductId).collect(Collectors.toSet());
        if(productSet.contains(productId)){
            throw new CommonException(201,"该商品您已点赞了");
        }
        //商品表点赞数加一
        product.setStar(product.getStar()+1);
        productService.updateById(product);
        this.save(new Star(user.getId(),productId));
    }

    @Override
    public List<Product> getStarList() {
        User user = TokenUtils.getCurrentAdmin();
        if(Objects.isNull(user)){
            throw new CommonException(201,"请登录后查看您的点赞列表");
        }
        Integer userId = user.getId();
        List<Star> starList = starMapper.selectList(new QueryWrapper<Star>().eq("user_id", userId));
        if(CollectionUtils.isEmpty(starList)){
            return null;
        }
        Set<Integer> productIdList = starList.stream().map(Star::getProductId).collect(Collectors.toSet());
        List<Product> productList=new ArrayList<>();
        for (Integer productId : productIdList) {
            Product product = productService.getById(productId);
            productList.add(product);
        }
        return productList;
    }
}
